package com.hdax.gulimall.product.service.impl;

import com.hdax.common.constant.ProductConstant;
import com.hdax.gulimall.product.entity.PmsAttrEntity;
import com.hdax.gulimall.product.service.PmsAttrService;
import com.hdax.gulimall.product.vo.AttrGroupRelationVo;
import com.hdax.gulimall.product.vo.AttrGroupWithAttrsVo;
import com.hdax.gulimall.product.vo.AttrResponseVo;
import com.hdax.gulimall.product.vo.SpuItemAttrGroupVo;
import org.apache.commons.lang.StringUtils;
import org.mockito.internal.util.StringUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hdax.common.utils.PageUtils;
import com.hdax.common.utils.Query;

import com.hdax.gulimall.product.dao.PmsAttrGroupDao;
import com.hdax.gulimall.product.entity.PmsAttrGroupEntity;
import com.hdax.gulimall.product.service.PmsAttrGroupService;


@Service("pmsAttrGroupService")
public class PmsAttrGroupServiceImpl extends ServiceImpl<PmsAttrGroupDao, PmsAttrGroupEntity> implements PmsAttrGroupService {


    @Autowired
    private PmsAttrService pmsAttrService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<PmsAttrGroupEntity> page = this.page(
                new Query<PmsAttrGroupEntity>().getPage(params),
                new QueryWrapper<PmsAttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 第一个参数封装了分页以及模糊查询的条件，第二个参数是分类的id
     * @param params
     * @param catlogId
     * @return
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catlogId) {

        String key= (String) params.get("key");
        QueryWrapper<PmsAttrGroupEntity> wrapper=new QueryWrapper<PmsAttrGroupEntity>();
        if(!StringUtils.isEmpty(key)){
            wrapper.and((object)->{
                object.eq("attr_group_id",key).or().like("attr_group_name",key);
            });
        }

        //如果所属分类的id是0那就查询全部
        if(catlogId==0){
            IPage<PmsAttrGroupEntity> page = this.page(new Query<PmsAttrGroupEntity>().getPage(params), wrapper);
            return new PageUtils(page);
        }else{
            wrapper.and((object)->{
                object.eq("catelog_id",catlogId);
            });
            IPage<PmsAttrGroupEntity> page=this.page(new Query<PmsAttrGroupEntity>().getPage(params),wrapper);
            return new PageUtils(page);
        }

    }


    /**
     * 根据分类id查出所有的分组以及这些组里面的属性
     * @param catelogId
     * @return
     */
    @Override
    public List<AttrGroupWithAttrsVo> getAttrGroupWithAttrs(Long catelogId) {

        //1、查询分组信息(三级分类下所有的分组)
        List<PmsAttrGroupEntity> catelog_id = this.list(new QueryWrapper<PmsAttrGroupEntity>().eq("catelog_id", catelogId));
        //2、查询所有的属性
        List<AttrGroupWithAttrsVo> collect = catelog_id.stream().map(item -> {
            AttrGroupWithAttrsVo attrGroupWithAttrsVo=new AttrGroupWithAttrsVo();
            BeanUtils.copyProperties(item,attrGroupWithAttrsVo);
            //根据分组的id查询所有属性
            List<PmsAttrEntity> relationAttr = pmsAttrService.getRelationAttr(item.getAttrGroupId());
            attrGroupWithAttrsVo.setAttrs(relationAttr);
            return attrGroupWithAttrsVo;
        }).collect(Collectors.toList());
        return collect;
    }

    /**
     * 根据spu的id查询属性的分组
     * @param spuId
     * @return
     */
    @Override
    public List<SpuItemAttrGroupVo> getAttrGroupAttrsBySpuId(Long spuId,Long catalogId) {
    //        # 首先查出属性分组，根据商品关联的分类id查询属性
    //# 再根据规格参数的id查询出规格参数
        List<SpuItemAttrGroupVo> list=baseMapper.getAttrGroupWithAttrsBySpuId(spuId,catalogId);
        return list;
    }



}